package com.the;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapDemo {
        public static void main(String[] args) {
        test04();
        }
        public static void test04(){
            Map<String, Double> map = new HashMap<>();
            map.put("小明", 389.0);
            map.put("张三", 361.0);
            map.put("李四", 357.0);
            map.put("王二", 416.0);

            map.forEach(new BiConsumer<String, Double>() {
                @Override
                public void accept(String key, Double value) {
                    System.out.println(key + "---" + value);
                }
            });
            map.forEach((key,value)-> System.out.println(key + "---" + value));
        }



        public static void test03(){
            Map<String, Double> map = new HashMap<>();
            map.put("小明", 389.0);
            map.put("张三", 361.0);
            map.put("李四", 357.0);
            map.put("王二", 416.0);
            //map集合转成set集合  key-value ->绑定为一个对象entry
            Set<Map.Entry<String, Double>> entrySet = map.entrySet();
            //遍历
            for (Map.Entry<String, Double> entry : entrySet) {
            //获取key
            String key = entry.getKey();
            //获取value
            Double value = entry.getValue();
            System.out.println(key + "---" + value);
            }
        }



        public static void test01(){
            Map<String, Double> map = new HashMap<>();
            map.put("小明", 389.0);
            map.put("张三", 361.0);
            map.put("李四", 357.0);
            map.put("王二", 416.0);

            Set<String> keySet = map.keySet();
            //遍历
            for (String key : keySet) {
                //根据key找值
                Double value = map.get(key);
                System.out.println(key + "---" + value);
            }
        }

        public static void test02(){

        Map<String, Double> map = new HashMap<>();
        map.put("小明", 389.0);
        map.put("张三", 361.0);
        map.put("李四", 357.0);
        map.put("王二", 416.0);

        System.out.println(map);
        map.remove("王二");
        System.out.println(map);
        map.put("李四", 357.0);
        System.out.println(map);

        boolean c1 = map.containsKey("小明");
        System.out.println("c1=" + c1);
        boolean c2 = map.containsValue(357.0);
        System.out.println("c2=" + c2);

        boolean empty = map.isEmpty();
        System.out.println("empty=" + empty);
        int size = map.size();
        System.out.println("size=" + size);

//        map.clear();
//        System.out.println(map);
    }
}

